package com.ss.android.article.common.g;

import android.content.Context;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.WindowManager;

import com.ss.android.newmedia.BaseApplication;

public final class ImageMeasure {
    public static final class Spec {
        public int mWidth;
        public int mHeight;

        public Spec(int arg1, int arg2) {
            this.mWidth = arg1;
            this.mHeight = arg2;
        }
    }

    private static final int MAX_SIZE_SINGLE_IMAGE;
    private static final int MIN_WIDTH_SINGLE_IMAGE;

    static {
        Display defaultDisplay = ((WindowManager) BaseApplication.getApplication().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
        DisplayMetrics displayMetrics = new DisplayMetrics();
        defaultDisplay.getMetrics(displayMetrics);
        MAX_SIZE_SINGLE_IMAGE = displayMetrics.widthPixels / 2;
        MIN_WIDTH_SINGLE_IMAGE = displayMetrics.widthPixels / 6;
    }

    public static void updateSingleImageSpecForWD(Spec arg12, int arg13) {
        double v10 = 4;
        double v8 = 3;
        double v6 = 2;
        int v0 = arg12.mWidth;
        int v1 = arg12.mHeight;
        int v2 = BaseApplication.getApplication().getResources().getDisplayMetrics().widthPixels - arg13;
        float v3 = computeRatio(arg12);
        if(v3 < 0.333333f) {
            arg12.mWidth = ((int)((((double)v2)) / v6));
            arg12.mHeight = ((int)((((double)v2)) * v8 / v10));
        }
        else if(v3 < 1f) {
            arg12.mHeight = ((int)((((double)v2)) / v6));
            arg12.mWidth = ((int)((((double)v0)) / (((double)v1)) * (((double)arg12.mHeight))));
        }
        else if(v3 < 3f) {
            arg12.mWidth = ((int)((((double)v2)) / v6));
            arg12.mHeight = ((int)((((double)v1)) / (((double)v0)) * (((double)arg12.mWidth))));
        }
        else {
            arg12.mHeight = ((int)((((double)v2)) / v6));
            arg12.mWidth = ((int)((((double)v2)) * v8 / v10));
        }
    }

    public static void updateSingleImageSpec(Spec spec) {
        float ratio = computeRatio(spec);
        if(ratio < 0.333333f) {
            spec.mWidth = MIN_WIDTH_SINGLE_IMAGE;
            spec.mHeight = Math.min(((int)((((float)spec.mWidth)) / ratio)), MAX_SIZE_SINGLE_IMAGE);
        }
        else if(ratio < 1f) {
            spec.mHeight = MAX_SIZE_SINGLE_IMAGE;
            spec.mWidth = ((int)(ratio * (((float)spec.mHeight))));
        }
        else {
            spec.mWidth = MAX_SIZE_SINGLE_IMAGE;
            spec.mHeight = ((int)((((float)spec.mWidth)) / ratio));
        }
    }

    public static void updateSingleImageSpec2(Spec spec, int arg9) {
        int v0 = spec.mWidth;
        int v1 = spec.mHeight;
        DisplayMetrics v2 = BaseApplication.getApplication().getResources().getDisplayMetrics();
        int v3 = v2.widthPixels - arg9;
        int v2_1 = v2.heightPixels;
        spec.mWidth = v3;
        spec.mHeight = ((int)((((double)v3)) / (((double)v0)) * (((double)v1))));
        if(spec.mHeight >= v2_1) {
            spec.mWidth = ((int)((((double)v3)) * 2 / 3));
            spec.mHeight = ((int)((((double)spec.mWidth)) * 1.5));
        }
    }

    public static float computeRatio(Spec spec) {
        float v1 = ((float)spec.mWidth);
        int v0 = spec.mHeight != 0 ? spec.mHeight : MAX_SIZE_SINGLE_IMAGE;
        return v1 / (((float)v0));
    }
}

